
/******************************************************************************
*@file  : app.c
*@brief : application
******************************************************************************/

#include "app.h" 




#define TIM_CLOCK_FREQ            (10000)  

TIM_HandleTypeDef TIMx_Handler;   
volatile uint32_t g_Timer_Update_Flag;

void HAL_TIM_Updeate_Event_Callback(TIM_HandleTypeDef *htim)
{
    g_Timer_Update_Flag = 1;   
}

/******************************************************************************
*@brief:  Initialize the selected timer.
*@param:  None
*@return: None
******************************************************************************/
void TIMx_Init(void)  
{
    uint32_t timer_clock; 
    
    timer_clock = HAL_RCC_GetPCLK1Freq();
    
    
    if (HAL_RCC_GetHCLKFreq() != timer_clock)  // if hclk/pclk != 1, then timer clk = pclk * 2  
    {
        timer_clock =  timer_clock << 1;    
    }
    
	TIMx_Handler.Instance = TIM6;
	TIMx_Handler.Init.ARRPreLoadEn = TIM_ARR_PRELOAD_ENABLE;    
	TIMx_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 
	TIMx_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; 
	TIMx_Handler.Init.RepetitionCounter = 0;  
	TIMx_Handler.Init.Prescaler = (timer_clock/TIM_CLOCK_FREQ) - 1;  
	TIMx_Handler.Init.Period = (TIM_CLOCK_FREQ/1000)*500 - 1;  // 500ms 
	
	HAL_TIMER_MSP_Init(&TIMx_Handler);       
	HAL_TIMER_Base_Init(&TIMx_Handler);    
	HAL_TIM_ENABLE_IT(&TIMx_Handler, TIMER_INT_EN_UPD);  
	
	HAL_TIMER_Base_Start(TIMx_Handler.Instance);            
}

/******************************************************************************
*@brief:  APP_Test.
*@param:  None
*@return: None
******************************************************************************/
void APP_Test(void)
{
    printfS("Timer6 Update Interrupt Test\n"); 
    
	g_Timer_Update_Flag = 0;  
    
	TIMx_Init();
    
	while(1)
	{
		if(g_Timer_Update_Flag) 
		{
            printfS("Timer6 Update Occurs\n");
            g_Timer_Update_Flag = 0;   
		}
	}
}

